package view;

import com.wedu.entity.Job;
import com.wedu.entity.User;
import com.wedu.service.UserService;
import com.wedu.util.TSUtility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Post_part_time_staff {
    private static Scanner sc = new Scanner(System.in);

    public static void post_part_time_staff(User user, UserService userService){
        System.out.println("**************************");
        System.out.println("***                    ***");
        System.out.println("***  欢迎" + user.getName() + "发布兼职人员***");
        System.out.println("***                    ***");
        System.out.println("**************************");
        while (true) {
            System.out.println("\t\t1.兼职信息投放\n\t\t2.已投放兼职信息查看" +
                    "\n\t\t3.兼职信息撤销"+"\n\t\t4.退出登录"+"\n\t\t5.退出系统");
            System.out.print("请输入你的选择：");
            char c = TSUtility.readMenuSelection();
            switch (c){
                case '1':
                    //兼职信息投放
                    information(user,userService);
                    break;
                case '2':
                    //兼职信息查看
                    look_over(userService,user.getACID());
                    break;
                case '3':
                    //兼职信息撤销
                    revoke(userService,user.getACID());
                    break;
                case '4':
                    System.out.println("退出成功！");
                    return;
                case '5':
                    System.out.print("你确定要退出系统吗？请输入（Y/N）：");
                    char a=TSUtility.readConfirmSelection();
                    TSUtility.loadSpecialEffects();
                    if (a=='Y'||a=='y'){
                        System.out.println("退出成功！欢迎下次使用！");
                        System.exit(0);
                    }else break;
            }
        }
    }
    private static void information(User user,UserService userService){
        System.out.println("...........兼职信息投放............");
        System.out.print("请输入公司营业执照编号：");
        String JID=sc.next();
        System.out.print("请输入公司名称：");
        String company_name=sc.next();
        System.out.print("请输入兼职类型：");
        String JobType_name=sc.nextLine();
        System.out.print("请输入职位名称：");
        String job_name=sc.next();
        int job_level;
        for (;;) {
            System.out.print("请输入兼职工作等级(1-5)：");
            try {
                job_level = Integer.parseInt(sc.next());
                if (job_level<1||job_level>5)
                    System.out.println("请按要求填写等级！");
                else break;
            }catch (NumberFormatException e){
                System.out.println("请按要求填写等级！");
            }
        }
        double salary;
        for (;;){
            System.out.print("请输入工资（单位：元/小时）：");
            try {
                salary=Double.parseDouble(sc.next());
                break;
            }catch (NumberFormatException e){
                System.out.println("请正确填写工资信息！");
            }
        }

        int demand;
        for (;;){
            System.out.print("请输入职位需求人数：");
            try{
                demand=Integer.parseInt(sc.next());
                break;
            }catch (NumberFormatException e){
                System.out.println("请正确填写职位需求人数！");
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH：mm");
        String job_time;
        while (true) {
            System.out.print("请输入工作开始时间(如：2022.8.10 13：00）：");
            job_time = sc.nextLine();
            try {
                Date dd = sdf.parse(job_time);
                break;
            } catch (ParseException e) {
                System.out.println("你输入的时间格式不正确，请正确填写！");
            }
        }
        String job1_time;
        while (true) {
            System.out.print("请输入工作结束时间(如：2022.8.10 13：00）：");
            job1_time = sc.nextLine();
            try {
                Date dd = sdf.parse(job1_time);
                break;
            }catch (ParseException e) {
                System.out.println("你输入的时间格式不正确，请正确填写！");
            }
        }
        System.out.print("请输入工作内容描述：");
        String job_content=sc.nextLine();
        double deduct_salary;
        for (;;) {
            System.out.print("请输入逾期费用扣除比例(如：0.15)：");
            try {
                deduct_salary = Double.parseDouble(sc.next());
                break;
            }catch (Exception e){
                System.out.println("请正确填写信息！");
            }
        }
        Job job = new Job();
        job.setJob_name(job_name);
        job.setJobType_name(JobType_name);
        job.setAcid(user.getACID());
        job.setJob_level(job_level);
        job.setSalary(salary);
        job.setDemand(demand);
        job.setJob_time(job_time);
        job.setJob1_time(job1_time);
        job.setDeduct_salary(deduct_salary);
        job.setJob_content(job_content);
        job.setStare("false");
        job.setJID(JID);
        job.setCompany_name(company_name);
        userService.information(job);
        System.out.println("兼职信息投放成功！等待管理员审批！");
    }

    private static void look_over(UserService userService,String acid){
        System.out.println("...........已投放兼职信息查看............");
        System.out.println("\t提示：只能查看自己投放的兼职信息，已投放的信息包含所有的兼职信息（已完成、逾期、未开始、进行中、待审批）");
        List<Job> jobs = userService.job_information(acid);
        if (jobs.size()==0){
            System.out.println("暂时没有任何兼职信息！\n");
            return;
        }
        System.out.println("兼职信息编号\t公司名称\t公司编号\t发布兼职人员ID 兼职类型 职位名称 职位等级 工资（元/小时） 需求人数\t工作开始时间\t\t结束时间\t逾期扣费比例\t职位审核状态\t职位工作状态\t工作描述");
        for (Job job:jobs) {
            String str = "";
            if (job.getApproval_status() == 1)
                str = "等待管理员审核！";
            if (job.getApproval_status() == 2)
                str = "管理员审核通过！";
            if (job.getApproval_status() == 3)
                str = "管理员审核未通过！";
            String str1="";
            if (job.getStare().equals("true")) {
                str1 = "进行中";
            }else {
                str1="未开始";
            }
            System.out.println(job.getDID() + "\t  " + job.getCompany_name() + "\t" + job.getJID() + "\t"+job.getAcid() + job.getJobType_name() + "\t " + job.getJob_name() +
                    "\t\t" + job.getJob_level() + "\t\t" + job.getSalary() + "\t\t" + job.getDemand() + "\t\t" + job.getJob_time() + " " + job.getJob1_time() + "  "
                    + job.getDeduct_salary() + "\t" + str + str1 + "\t" + job.getJob_content());
        }
    }

    private static void revoke(UserService userService,String acid){
        System.out.println("...........兼职信息撤销............");
        System.out.println("\t提示：只能撤销自己投放的兼职信息，且进行中的信息撤销会自动计算费用给相应的兼职人员！");
        int did=TSUtility.did();
        Job job = userService.information(did, acid);
        if (job==null){
            System.out.println("你输入的兼职信息编号不存在！");
            return;
        }
        System.out.print("你确定要撤销兼职信息吗？请输入（Y/N）：");
        char a=TSUtility.readConfirmSelection();
        TSUtility.loadSpecialEffects();
        if (a=='Y'||a=='y'){
            revoke_t(job,userService,did);
            System.out.println("兼职信息撤销成功！");
        }else
            System.out.println("你选择了NO，兼职信息未撤销！");
    }

    public static void revoke_t(Job job,UserService userService,int did){
        if (job.getStare().equals("true")){
            //获取当前时间毫秒值
            long time=System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH：mm");
            long time1=0;
            long time2=0;
            try {
                //获取兼职开始时间毫秒值
                Date dd = sdf.parse(job.getJob_time());
                time1=dd.getTime();
                //获取兼职结束时间
                Date dd1 = sdf.parse(job.getJob1_time());
                time2=dd1.getTime();
            }catch (Exception e){
                e.fillInStackTrace();
            }
            long time3=time/1000/60/60-time1/1000/60/60;
            if (time3>0){
                long time4=time/1000/60/60-time2/1000/60/60;
                double salary=0;
                if (time4>0)
                    salary=job.getSalary()*(time2/1000/60/60-time1/1000/60/60)-job.getDeduct_salary()*time4;
                else
                    salary=job.getSalary()*time3;
                List<String> list = userService.job_acid(did);
                if (list!=null){
                    for (String id:list){
                        userService.user_balance(id,salary);
                    }
                }
            }
        }
        userService.revoke(did);

    }
}
